package com.hq.fxh.Utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :
 * @Created 2017/8/14
 * @Email xlwenhao1858@sina.com
 */


public class Tool {
    /*判断按钮是否连击
    * */
    private static final int MIN_CLICK_DELAY_TIME = 1000;
    private static long lastClickTime;

    public static boolean isFastClick() {
        boolean flag = false;
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
            flag = true;
        }
        lastClickTime = curClickTime;
        return flag;
    }

    /*设置屏幕透明度*/
    public static void setAlpha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().setAttributes(lp);
    }

    // 判断edit是否为空
    public static boolean editIsEmpty(EditText ed) {
        boolean empty = TextUtils.isEmpty(ed.getText());
        if (empty) {
            return true;
        }
        String s = ed.getText().toString();
        Log.e("字符串", s);
        for (char c : s.toCharArray()) {
            if (c != 32) {

                return false;
            }
        }
        return true;
    }

    public static int getIntFromString(String str) {
        int a = 0;
        try {
            a = Integer.parseInt(str);
        } catch (NumberFormatException e) {
            LogUtils.e("String 转 int 错误=" + str);
        }
        return a;
    }

    public static float getFloatFromString(String str) {
        float a = 0;
        try {
            a = Float.parseFloat(str);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            LogUtils.e("String 转 float 错误=" + str);

        }
        return a;
    }


    // 给textview赋值
    public static void setStringToText(TextView text, String stringContent) {
        try {
            if (stringContent == null || stringContent.equals("")) {
                LogUtils.e("string为空");
            } else {
                text.setText(stringContent);
            }
        } catch (Exception e) {
            LogUtils.e(stringContent + "赋值报错");

        }
    }

    public static void setIntToText(TextView text, int item_count) {
        try {
            if (item_count <= 0) {
                LogUtils.e(item_count + "小于0");
                text.setText(item_count + "");

            } else {
                text.setText(item_count + "");
            }
        } catch (Exception e) {
            LogUtils.e(item_count + "赋值报错");

        }
    }

    public static void setStringToEdit(EditText text, String stringContent) {
        try {
            if (stringContent == null || stringContent.equals("")) {
                text.setText("");
            } else {
                text.setText(stringContent);
            }
        } catch (Exception e) {
            LogUtils.e(stringContent + "赋值报错");

        }
    }

    //给imageview赋值
    public static void setUrlToImage(Context context, String stringContent, ImageView imageView) {
        try {
            if (stringContent == null || stringContent.equals("")) {
                LogUtils.e("ImageView为空");
            } else {
                GlideUtils.loadImageView(context, stringContent, imageView);
            }
        } catch (Exception e) {
            LogUtils.e(stringContent + "赋值报错");

        }
    }

    public static void setUrlToImage1(Context context, String stringContent, ImageView imageView) {
        try {
            if (stringContent == null || stringContent.equals("")) {
                LogUtils.e("ImageView为空");
            } else {
                GlideUtils.loadImageView(context, stringContent, imageView);
            }
        } catch (Exception e) {
            LogUtils.e(stringContent + "赋值报错");

        }
    }

    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);

    }


    public static void openActivity(Context context, Class<?> pClass, Bundle pBundle) {
        Intent intent = new Intent(context, pClass);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        context.startActivity(intent);
    }





}
